package com.heying.springbootweb.restcontroller;

import com.heying.springbootweb.entity.Course;
import com.heying.springbootweb.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

@RestController
@RequestMapping(path = "/rest/cou",produces = "application/json")
@Slf4j
@CrossOrigin(origins = "*")
public class CourseRestController {

    private List<Course> courses;


    {
        courses = new ArrayList<>();
        courses.add(new Course(1,"1001","高数","44","images/mm.jpg"));
        courses.add(new Course(2,"1002","线代","55","images/mm.jpg"));
        courses.add(new Course(3,"1003","英语","66","images/mm.jpg"));
        courses.add(new Course(4,"1004","马原","77","images/mm.jpg"));
        courses.add(new Course(5,"1005","毛概","88","images/mm.jpg"));
    }
    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<Course> queryAll(){
        return courses;
    }

    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Course findOne(@PathVariable Integer id){
        return courses.stream()
                .filter(s -> s.getId().equals(id))
                .findFirst().orElseThrow(NoSuchElementException::new);
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Course addOne(@RequestBody Course cou){
        cou.setId(courses.stream().mapToInt(Course::getId).
                max().orElse(0)+1);
        courses.add(cou);
        log.debug("course is {}",courses);
        return cou;
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public void modOne(@PathVariable Integer id,@RequestBody Course cou){
        courses.stream().filter(s->s.getId().equals(id)).findFirst()
                .ifPresent(s-> BeanUtils.copyProperties(cou, s));
        log.debug("course is {}",courses);
    }
    @PatchMapping("/{id}/{cname}")
    @ResponseStatus(HttpStatus.OK)
    public void modOneName(@PathVariable Integer id,@PathVariable String cname){
        courses.stream().filter(s->s.getId().equals(id))
                .findFirst().ifPresent(s->s.setCname(cname));
        log.debug("course is {}",courses);
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Course delOne(@PathVariable Integer id){
        Course cou = null;
        for(Iterator<Course> it = courses.iterator(); it.hasNext();){ //iterator和foreach的区别
            Course c = it.next();
            if(c.getId().equals(id)){
                cou = c;
                it.remove();
            }
        }
        if(cou == null){
            throw new NoSuchElementException();
        }
        log.debug("course is {}",courses);
        return cou;
    }

    @GetMapping("/delete/{id}")
//    @ResponseStatus(HttpStatus.NO_CONTENT)
    @ResponseStatus(HttpStatus.OK)
    public Course delOne2(@PathVariable Integer id){
        Course cou = null;
        for(Iterator<Course> it = courses.iterator(); it.hasNext();){
            Course c = it.next();
            if(c.getId().equals(id)){
                cou = c;
                it.remove();
            }
        }
        if(cou == null){
            throw new NoSuchElementException();
        }
        log.debug("stu is {}", cou);
        log.debug("course is {}", courses);
        return cou;
    }

}
